#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: changerecords_parse.py
@time: 2018/2/5 12:02
"""

import sys

sys.path.append('../..')
from common.mongo import MongDb
from logger import Logger

APP_DATA_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('changerecords_parse.log').get_logger()

app_data_db = MongDb(APP_DATA_CONF['host'], APP_DATA_CONF['port'], APP_DATA_CONF['db'],
                     APP_DATA_CONF['username'],
                     APP_DATA_CONF['password'], log=log)


def main():
    source_table = 'enterprise_data_gov_new_2018_01_08'
    for item in app_data_db.traverse_batch_field(source_table, {},
                                                 ['changerecords', 'company', 'province']):

        company = item.get('company')

        changerecords = item.get('changerecords')
        if not isinstance(changerecords, list):
            continue

        if len(changerecords) <= 0:
            continue

        for result_item in changerecords:
            change_date = result_item.get('change_date')
            change_title = result_item.get('change_item')
            log.info("date = {} company = {} title = {}".format(change_date, company, change_title))


if __name__ == '__main__':
    main()
